<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>sessionStorage</title>
</head>
<body>
<h2>sessionStorage</h2>
<button onclick="saveData()">点我保存一个数据</button>
<button onclick="readData()">点我读取一个数据</button>
<button onclick="deleteData()">点我删除一个数据</button>
<button onclick="clearAll()">点我清空所有数据</button>

<script type="application/javascript">
  function saveData() {
    /**
     * sessionStorage的内容关闭浏览器后会消失
     */
    sessionStorage.setItem('msg','helloWorld')
    sessionStorage.setItem('msg2',666)
    let person = {name:'张三',age:19}
    sessionStorage.setItem('person',JSON.stringify(person))
  }
  function readData() {
    let msg = sessionStorage.getItem('msg')
    let msg2 = sessionStorage.getItem('msg2')
    let person = sessionStorage.getItem('person')
    console.log(msg)
    console.log(msg2)
    console.log(person)
    console.log(JSON.parse(person))
  }
  function deleteData() {
    sessionStorage.removeItem('msg')
    alert('已删除')
  }
  function clearAll(){
    sessionStorage.clear()
  }
</script>
</body>
</html>
